//
//  MainTabBarController.swift
//  WeiboProject
//
//  Created by amos on 16/1/18.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.设置自定义 tabBar
        let tabBar = MainTabBar.mainTabBar()
        self.setValue(tabBar, forKey: "tabBar")
        
        //2.定义闭包
        tabBar.composeBtnClourse = {
            print("老子开始写作啦````")
        }
    
        //3.设置背景图片---解决 push 时黑影
        tabBarItem.image = UIImage(named: "searchbar_textfield_background")
        
        setUpUI()
    }
    
    func setUpUI() {
        //1.添加子控制器
        initChildViewControllers(UINavigationController(rootViewController: HomeTableViewController()), title: "Home", imageName: "tabbar_home")
        initChildViewControllers(UINavigationController(rootViewController: MessageTableViewController()), title: "Message", imageName: "tabbar_message_center")
        initChildViewControllers(UINavigationController(rootViewController: DiscoverTableViewController()), title: "Discover", imageName: "tabbar_discover")
        initChildViewControllers(UINavigationController(rootViewController: ProfileTableViewController()), title: "Me", imageName: "tabbar_profile")
        
       
    }
    
    //设置自控制器的 tabBar 相关属性
    func initChildViewControllers(childViewController: UINavigationController, title: String, imageName: String){
        
        //1.设置title
        //childViewController.tabBarItem.title = title
        
        childViewController.childViewControllers[0].navigationItem.title = title
        childViewController.tabBarItem.title = title
        
        
        //1.1 设置 tintColor
        childViewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //2.设置默认图片

        childViewController.tabBarItem.image = UIImage(named: imageName)
        
        //3.设置选中图片
        childViewController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //4.添加到 tabBarController
        self.addChildViewController(childViewController)
        
    }

}
